package com.michael.demos.springboot.swagger3.controller;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * 类功能描述:
 * <pre>
 *   xxxx
 * </pre>
 *
 * @author Michael
 * @version 1.0
 * @date 2020/9/22 16:16
 */
@RestController
public class RocketController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    // 发送给Broker，默认会自动创建topic，topic和tag用冒号分隔
    @GetMapping("/rocket/send")
    public String rocketSend() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        LocalDateTime currentTime = LocalDateTime.now();
        SendResult send = rocketMQTemplate.getProducer()
                                          .send(new Message("rocket-topic-1", "test", "abc", "Hello".getBytes()));
        System.out.println(send);

        rocketMQTemplate.convertAndSend("rocket-topic-1:tag1", currentTime.toString());
        rocketMQTemplate.convertAndSend("rocket-topic-1:MM", "Hello Michael ~");
        return currentTime.toString();
    }

    // 延时消息，RocketMQ支持这几个级别的延时消息，不能自定义时长
    // 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
    @GetMapping("/rocket/delayMsg/send")
    public String rocketDelayMsgSend() {
        LocalDateTime currentTime = LocalDateTime.now();
        rocketMQTemplate
                .syncSend("rocket-topic-1:tag-2", MessageBuilder.withPayload(currentTime.toString()).build(), 2000, 3);
        return currentTime.toString();
    }
}
